package com.smartfarm;

import com.smartfarm.domain.equipment.AgriculturalEquipment;
import com.smartfarm.domain.environment.EnvironmentData;
import com.smartfarm.domain.equipment.EquipmentStatus;
import com.smartfarm.domain.state.IdleState;
import com.smartfarm.domain.state.WorkingState;
import com.smartfarm.domain.strategy.EmergencyPathStrategy;
import com.smartfarm.domain.strategy.NormalPathStrategy;
import com.smartfarm.domain.task.Task;
import com.smartfarm.factory.HarvesterFactory;
import com.smartfarm.factory.SeederFactory;
import com.smartfarm.observer.EnvironmentMonitor;
import com.smartfarm.service.EquipmentManager;
import com.smartfarm.service.TaskManager;
import com.smartfarm.service.SystemLogger;

import java.time.LocalDateTime;
import java.util.Arrays;

public class SmartFarmDemo {
    public static void main(String[] args) {
        // 初始化管理器
        EnvironmentMonitor monitor = new EnvironmentMonitor();
        EquipmentManager equipmentManager = new EquipmentManager();
        TaskManager taskManager = new TaskManager();

        // 创建并注册设备
        SeederFactory seederFactory = new SeederFactory();
        HarvesterFactory harvesterFactory = new HarvesterFactory();

        // 创建智能播种机
        AgriculturalEquipment seeder = seederFactory.createEquipment("S001", "智能播种机");
        // 创建智能收割机
        AgriculturalEquipment harvester = harvesterFactory.createEquipment("H001", "智能收割机");

        // 注册设备
        equipmentManager.registerEquipment(seeder);
        equipmentManager.registerEquipment(harvester);

        // 将设备添加为环境观察者
        monitor.addObserver(seeder::updateEnvironmentData);
        monitor.addObserver(harvester::updateEnvironmentData);

        // 设置初始状态和策略
        seeder.setState(new IdleState());
        harvester.setState(new IdleState());
        seeder.setPathStrategy(new NormalPathStrategy());
        harvester.setPathStrategy(new NormalPathStrategy());

        // 创建作业任务
        Task seedingTask = Task.builder()
            .taskId("T001")
            .taskName("春季播种")
            .taskType("播种")
            .fieldId("F001")
            .equipmentIds(Arrays.asList("S001"))
            .startTime(LocalDateTime.now())
            .endTime(LocalDateTime.now().plusHours(8))
            .completionRate(0.0)
            .status("已创建")
            .priority("高")
            .description("1号田春小麦播种")
            .build();

        // 创建任务
        taskManager.createTask(seedingTask);
        SystemLogger.logInfo("任务管理器", "创建新播种任务：" + seedingTask.getTaskName());

        // 更新设备状态
        EquipmentStatus seederStatus = EquipmentStatus.builder()
            .equipmentId(seeder.getId())
            .latitude(35.123456)
            .longitude(115.123456)
            .fuelLevel(95.5)
            .speed(5.2)
            .workProgress(0.0)
            .healthStatus("良好")
            .operationMode("自动")
            .currentTask(seedingTask.getTaskId())
            .build();

        // 更新设备状态
        equipmentManager.updateEquipmentStatus(seeder.getId(), seederStatus);
        SystemLogger.logInfo("设备管理器", "已更新播种机状态");

        // 模拟正常环境下的操作
        System.out.println("\n=== 正常运行状态 ===");
        EnvironmentData normalCondition = EnvironmentData.builder()
            .soilMoisture(60.0)
            .soilPH(6.5)
            .soilNutrient(75.0)
            .temperature(25.0)
            .humidity(65.0)
            .windSpeed(3.0)
            .rainfall(0.0)
            .cropHeight(15.0)
            .cropDensity(80.0)
            .cropHealth("良好")
            .build();

        // 更新环境数据
        monitor.updateEnvironmentData(normalCondition);
        SystemLogger.logInfo("环境监测器", "已更新环境数据 - 正常条件");

        // 开始作业
        seeder.setState(new WorkingState());
        seeder.getState().handleTask(seeder);
        taskManager.updateTaskStatus(seedingTask.getTaskId(), "进行中", 10.0);

        // 模拟恶劣天气条件
        System.out.println("\n=== 恶劣天气状况 ===");
        EnvironmentData adverseCondition = EnvironmentData.builder()
            .soilMoisture(85.0)
            .soilPH(6.5)
            .soilNutrient(75.0)
            .temperature(32.0)
            .humidity(90.0)
            .windSpeed(12.0)
            .rainfall(25.0)
            .cropHeight(15.0)
            .cropDensity(80.0)
            .cropHealth("受压")
            .build();

        // 更新环境数据
        monitor.updateEnvironmentData(adverseCondition);
        SystemLogger.logWarning("环境监测器", "检测到恶劣天气条件");

        // 切换到紧急策略
        seeder.setPathStrategy(new EmergencyPathStrategy());
        harvester.setPathStrategy(new EmergencyPathStrategy());
        SystemLogger.logInfo("设备管理器", "已切换至紧急路径规划策略");

        // 暂停任务
        taskManager.updateTaskStatus(seedingTask.getTaskId(), "已暂停", 15.0);
        SystemLogger.logWarning("任务管理器", "由于恶劣天气任务已暂停");

        // 打印系统日志
        System.out.println("\n=== 系统日志 ===");
        SystemLogger.printLogs();
    }
} 